var externalDeviceJs = function(targetObj){
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
};

externalDeviceJs.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find(".search-panel"));
			var bindFunctionArray = [{fName:"queryInfo",fs:this.findGridData},{fName:"resetInfo",fs:this.resetInfo}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.createGrid();
		},
		//创建Grid
		createGrid:function(){
			var columns = this.gridColumns();
			var that = this;
			this.sysUtils.createGird(this.targetObj.find("#extDeviceList"), {
				dataSource:[],
				selectable: "single",
				reorderable: true,
				columnMenu: true,
				resizable: true,
				pageable: {input: true,
					numeric: false},
					columns: columns,
					dataBound:function(){
						that.targetObj.find("#extDeviceList").find("tr:gt(0)").unbind("dblclick");
						that.targetObj.find("#extDeviceList").find("tr:gt(0)").dblclick(function(e){
							var grid = that.targetObj.find("#extDeviceList").data("kendoGrid");
							var dataItem = grid.dataItem(grid.select());
							//打开查看窗口
							that.openExtDeviceWindow(dataItem["deviceCode"]);
						});	
					}
			});
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
		},
		findGridData:function(e){
			var that = this;
			if(isNotEmpt(e)){
				that = $(e.target).data("that");
			}
			if(validityForm(that.targetObj)){
				var jsonObj = that.sysUtils.getBindDocJson();
				//转换时间格式
				if(isNotEmpt(jsonObj["beginTime"])){
					jsonObj["beginTime"] = kendo.toString(jsonObj["beginTime"],'yyyy-MM-dd HH:mm:ss');
				}
				if(isNotEmpt(jsonObj["endTime"])){
					jsonObj["endTime"] = kendo.toString(jsonObj["endTime"],'yyyy-MM-dd HH:mm:ss');
				}
				var kendoGrid  = that.targetObj.find("#extDeviceList").data("kendoGrid");
				var urls = baseApiPath +"externalDevice/pagingList";
				var dataT = {curdatetime:new Date().getTime(),json:JSON.stringify(jsonObj)};
				var dataSource = that.sysUtils.getDataSource({url:urls,data:dataT,dataType:"odata"});
				kendoGrid.setDataSource(dataSource); 
			}
		},
		/*
		 * 加载list 列表
		 */
		gridColumns:function(){
			var  items=[{
				width:30,
				headerTemplate: '<input type="checkbox" gridId="extDeviceList" onclick="gridCheckAll(this)" />',
				template: '<input name="userCheck" type="checkbox" value="${deviceCode}"/>'
			},{
				title: "设备名称",
				field:"deviceName"
			},{
				title: "设备编号",
				field: "deviceCode"
			},{
				title: "设备类型",
				field: "deviceTypeName"
			},{
				title:"设备机号",
				field:"deviceNumber"
			},{
				title: "IP地址",
				field: "ipAddress",
			},{
				title: "端口号",
				field: "port",
			},{
				title: "创建时间",
				field: "createTime"
			},{
				title: "创建人",
				field:"createUserid"
			}];
			return items;
		},
		/**
		 * 弹出用户新增、修改或者查询界面
		 */
		openExtDeviceWindow:function(extDeviceCode,isEdit){
			var that = this;
			var titles = "";
			that.editResult = null;
			that.isEdit = null;
			var openwidonw = function(){
				that.userDivId = that.sysUtils.openWindow(that.userDivId,{
					title:titles,
					height:"500px",
					width:"750px",
					resizable:false,
					content:"views/parkLotCenter/facilitiesManage/externalDevice_Edit.html"
				}, true);
			};
			var ajaxPost = function(){
				JsonpAjax(baseApiPath+"externalDevice/queryExtDeviceInfo?extDeviceCode=" + extDeviceCode,null,"json",function(result){
					that.editResult = result;
					openwidonw();
				});
			};
			if(undefined == extDeviceCode && null == isEdit){  //表示新增界面
				titles = "新增外接设备";
				that.isEdit = false;
				openwidonw();
			}else if(true == isEdit){  //编辑界面
				//获取选中行的用户编号
				var grid = this.targetObj.find("#extDeviceList").data("kendoGrid");
				var dataItem = grid.dataItem(grid.select());
				if(null == dataItem){
					notification({ title:"操作失败",message:"请选择至少一条需要修改的记录!!"},'info');
					return;
				}
				extDeviceCode = dataItem["deviceCode"];
				titles = "编辑外接设备";
				that.isEdit = true;
				ajaxPost();
			}else{    //查询界面
				titles = "查询外接设备";
				that.isEdit = false;
				ajaxPost();
			}
		},
		/**
		 * 删除用户信息，只做逻辑删除
		 */
		deleteExtDevice:function(){
			var that = this;
			//获取呗选中的选择框
			var extDeviceCodeList = "";
			that.targetObj.find("input[name='userCheck']:checked").each(function(){
				extDeviceCodeList += $(this).val() + SEPARATOR;
			});
			if("" == extDeviceCodeList){
				notification({ title:"操作失败",message:"请选择至少一条需要删除的记录!!"},'info');
				return;
			}
			notification({ title:"确认信息",message:"确定需要删除选中的外接设备信息？"},'confirm',{callback:function(){
				extDeviceCodeList = extDeviceCodeList.substring(0, extDeviceCodeList.length - 1);
				JsonpAjax(baseApiPath+"externalDevice/deleteExtDevice",extDeviceCodeList,"json",function(result){
					that.findGridData();
				});
			}});
		}
};


var externalDeviceEdit = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.loadFlag = false;
	this.init();
	return this;
};

externalDeviceEdit.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var bindFunctionArray = [{fName:"save_info",fs:this.saveExtDevice},{fName:"closeWindow",fs:closeWindow}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.createDropDowmList();
		},
		//创建下拉列表框
		createDropDowmList:function(){
			//数字框
			this.targetObj.find("#deviceNumber").kendoNumericTextBox({
				min:100,
				max:127,
				format:"#"
			});
			this.targetObj.find("#port").kendoNumericTextBox({
				min:0,
				max:65535,
				format:"#"
			});
			var that = this;
			JsonpAjax(baseApiPath + "externalDevice/singleDevice",null,"json",function(result){
				that.targetObj.find("#deviceType").kendoDropDownList({
					dataTextField : "deviceName",
					dataValueField : "deviceType",
					dataSource: result,
					index:0,
					dataBound:function(){
						if(false == that.loadFlag){
							that.sysUtils.setBindFieldValue({deviceType:this.value()});//设置默认值
							that.loadFlag = true;
							//加载选中的设备参数界面
							that.loadDeviceParamsHtml(this.value());
						}
					},
					change:function(e){
						that.loadDeviceParamsHtml(this.value());
					}
				});
				that.loadData();
			});
		},
		/**
		 * 根据选择的设备类型加载参数界面
		 */
		loadDeviceParamsHtml:function(deviceType){
			var loadExtDeviceParams = this.targetObj.find("#loadExtDeviceParams");
			loadExtDeviceParams.empty();
			var that = this;
			var loadCallback = function(){
				if(null != extDeviceJs.editResult){
					//赋值
					var paramsJson = that.conversionParamsObjectToJson(extDeviceJs.editResult["deviceParamSettingSet"]);
					deviceParmasJs.setParamsValue(paramsJson);
					if(false == extDeviceJs.isEdit){  //查看模式
						deviceParmasJs.disabledHtml();
					}
				}
			}
			switch (parseInt(deviceType)) {
			case 11:      //IC卡发行器
				loadExtDeviceParams.load("views/parkLotCenter/deviceParams/ICCardReader.html",loadCallback);
				break;
			case 10:      //临时卡计费器
				loadExtDeviceParams.load("views/parkLotCenter/deviceParams/tempCardCharg.html",loadCallback);
				break;
			}
		},
		loadData:function(){
			if(null != extDeviceJs.editResult){
				this.sysUtils.setBindFieldValue(extDeviceJs.editResult);
				//设备类型不允许更改
				this.targetObj.find("#deviceType").data("kendoDropDownList").enable(false);
				if(false == extDeviceJs.isEdit){ //查看模式
					this.targetObj.find("input").attr("disabled","disabled");
					this.targetObj.find("textarea").attr("disabled","disabled");
					this.targetObj.find("#deviceNumber").data("kendoNumericTextBox").enable(false);
					this.targetObj.find("#port").data("kendoNumericTextBox").enable(false);
					this.targetObj.find(".basicInfo-btn").remove();
				}
			}
		},
		//保存子系统信息
		saveExtDevice:function(e){
			var that =  $(e.target).data("that");
			if(validityForm(that.targetObj)){
				var extDeviceInfo = that.sysUtils.getBindDocJson();
				var paramsJson = deviceParmasJs.getDeviceParmasJson();
				if(null == paramsJson) return;
				extDeviceInfo["deviceParamSettingSet"] = that.conversionParamsJsonToObject(paramsJson);
				var url = baseApiPath+"externalDevice/save";
				if(null != extDeviceJs.editResult){
					extDeviceInfo["deviceCode"] = extDeviceJs.editResult["deviceCode"];
					url = baseApiPath+"externalDevice/update";
				}
				JsonpAjax(url,JSON.stringify(extDeviceInfo),"json",function(result){
					closeWindow(); //关闭窗口
					extDeviceJs.findGridData();
				}); 
			}
		},
		/**
		 * 把json对象拆成竖表，一个属性转换成一个对象
		 * @param paramsJson
		 */
		conversionParamsJsonToObject:function(paramsJson){
			var paramsSettingArray = new Array();
			var paramsSettingObj = null;
			var group = null;
			for(var names in paramsJson){
				group = names.split(SEPARATOR);
				paramsSettingObj = new Object(); 
				paramsSettingObj["displayKey"] = group[2];
				paramsSettingObj["displayValue"] = paramsJson[names];
				paramsSettingObj["paramSettingsGroup"] = group[0];
				paramsSettingObj["groupingCode"] = group[1];
				paramsSettingArray.push(paramsSettingObj);
			}
			return paramsSettingArray;
		},
		/**
		 * 把Object拆成json对象， 一个对象转换成一个属性
		 */
		conversionParamsObjectToJson:function(paramsObjectArray){
			var jsonObj = new Object();
			var fieldName = null;
			var paramsObject = null;
			for(var i = 0; i < paramsObjectArray.length; i++){
				paramsObject = paramsObjectArray[i];
				fieldName = paramsObject["paramSettingsGroup"] + SEPARATOR + paramsObject["groupingCode"] + SEPARATOR + paramsObject["displayKey"];
				jsonObj[fieldName] = paramsObject["displayValue"];
			}
			return jsonObj;
		}
		
};